#!/bin/bash
# vim:fdm=marker fmr=[[[,]]] cms=\ #%s

[[ -z $SOAR ]] && SOAR=/opt/soar
PATH=$PATH:$SOAR/common/bash/soar
. soar.sh

load_ubbconfig()
{ #[[[
    local ubbconfig=$1

    local ipckey=$(cnfg_get_property $SVCENV_CNFG GLOBAL IPCKEY)
    if [[ -z $ipckey ]]
    then
        echo "Error: get IPCKEY from $SVCENV_CNFG fail"
        return 1
    fi
    echo "\
*RESOURCES
IPCKEY          $ipckey

DOMAINID        svcenv
MASTER          $HOSTNAME
MAXACCESSERS    1000
MAXSERVERS      200
MAXSERVICES     1500
MODEL           SHM
LDBAL           N

*MACHINES
DEFAULT:
    TUXDIR=\"$TUXDIR\"
    TUXCONFIG=\"$SVCENV_DIR/etc/tuxconfig\"
    APPDIR=\"$SVCENV_DIR/bin\"
    TLOGDEVICE=\"$SVCENV_DIR/etc/TLOG\"
    TLOGSIZE=400
    ULOGPFX=\"$SVCENV_DIR/log/ULOG\"

    $HOSTNAME LMID=$HOSTNAME
    MAXWSCLIENTS=200
" >> $ubbconfig

    local id

    local sn_secure_addr=$(cnfg_get_subproperty $SVCENV_CNFG SVCNODE SECURE ADDR)
    if [[ -z $sn_secure_addr ]]
    then
        echo "Error: get SECURE from $SVCENV_CNFG fail"
        return 1
    fi
    echo "*GROUPS" >> $ubbconfig
    local db
    id=1
    for db in $(cnfg_property_array $SVCENV_CNFG DB)
    do
        local dbuser=$(cnfg_get_subproperty $SVCENV_CNFG DB $db USER)
        if [[ -z $dbuser ]]
        then
            echo "Error: $db USER not set"
            return 1
        fi

        local respond=$(sn_call "$sn_secure_addr" GETDBPASSWD "$db" "$dbuser")
        if [[ -z $respond ]]
        then
            echo "Error: connect to sn_secure fail"
            return 1
        fi
        local curr=0
        local item=$(para_get_item "$respond" $curr)
        if [[ -z $item || $item != "0000" ]]
        then
            echo "Error: get passwd($db:$dbuser) from sn_secure fail, errcode:$item"
            return 1
        fi
        (( curr+=$(para_item_size "$item") ))
        local dbpasswd=$(para_get_item "$respond" $curr)
        if [[ -z $dbpasswd ]]
        then
            echo "Error: passwd($db:$dbuser) invalid"
            return 1
        fi

        local host=$(cnfg_get_subproperty $SVCENV_CNFG DB $db HOST)

        echo -n "\
$db
    LMID=$HOSTNAME GRPNO=$id
    TMSNAME=TMS_ORA TMSCOUNT=2
    OPENINFO=\"Oracle_XA:Oracle_XA+ACC=P/$dbuser/$dbpasswd+SesTM=120+LogDir=$SVCENV_DIR/log" >> $ubbconfig
        if [[ -n $host ]]
        then
            echo -n "+Sqlnet=$host" >> $ubbconfig
        fi
        echo "\"" >> $ubbconfig
        echo >> $ubbconfig

        (( id+=1 ))
    done
    echo >> $ubbconfig

    echo "*SERVERS
DEFAULT:
    CLOPT=\"-A\"
" >> $ubbconfig
    id=10
    for svr in $(cnfg_property_array $SVCENV_CNFG SERVER)
    do
        local db=$(cnfg_get_subproperty $SVCENV_CNFG SERVER $svr DB)
        if [[ -z $db ]]
        then
            echo "Error: $svr DB not set"
            return 1
        fi
        local start=$(cnfg_get_subproperty $SVCENV_CNFG SERVER $svr START)
        if [[ -z $start ]]; then start=1; fi
        echo "\
$svr SRVGRP=$db SRVID=$id MIN=$start RQADDR=\"${HOSTNAME}_${svr#svr_}\" REPLYQ=Y" >> $ubbconfig
        (( id+=start ))
    done
    echo >> $ubbconfig

    echo "*SERVICES" >> $ubbconfig

    tmloadcf -y $ubbconfig
    if (( $? != 0 ))
    then
        echo "Error: tmloadcf $ubbconfig fail"
        return 1
    fi

    echo "Load ubbconfig succeed"
} #]]]

create_tlog()
{ #[[[
    if [[ -f TLOG ]]
    then
        return 0
    fi

    tmadmin <<!
echo
crdl -b 500 -z $SVCENV_DIR/etc/TLOG
crlog -m $HOSTNAME
q
!
    if (( $? != 0 ))
    then
        echo "Error: tmadmin crlog fail"
        return 1
    fi

    echo "Create TLOG succeed"
} #]]]

main()
{ #[[[
    if [[ -z $SVCENV_DIR ]]
    then
        echo "Error: SVCENV_DIR not set"
        return 1
    fi
    if [[ -z $SVCENV_CNFG ]]
    then
        echo "Error: SVCENV_CNFG not set"
        return 1
    fi
    cd $SVCENV_DIR/etc

    local ubbconfig=_ubbconfig_tmp
    rm -f $ubbconfig
    load_ubbconfig $ubbconfig
    local res=$?
    rm -f $ubbconfig
    if (( $res != 0 ))
    then
        return 1
    fi

    echo "--------------------------------------------------"

    create_tlog
    if (( $? != 0 ))
    then
        return 1
    fi
} #]]]

LANG=C
main $@
exit $?
